<template>
  <j-modal :title="title" :visible="visible" @cancel="handleCancel" @ok="handleOk" width="30%" :okText="$t('保存')" :cancelText="$t('关闭')" v-drag>
    <div>
      <a-row style="margin-left: 10px">
        <a-col :span="24">
          <a-form-model
            :rules="rules"
            :model="dataForm"
            ref="form"
            :label-col="labelCol"
            :wrapper-col="wrapperCol"
            v-enterToNext
          >
            <a-row v-if="payType_state">
              <a-form-model-item :label="$t('支付方式')">
                <div style="font-weight: 600">{{ dataForm.nameText }}</div>
              </a-form-model-item>
            </a-row>
            <a-row v-if="!payType_state">
              <a-form-model-item :label="$t('支付方式')" prop="nameText">
                <a-input v-model="dataForm.nameText"></a-input>
              </a-form-model-item>
            </a-row>
            <a-row v-if="dataForm.name == 2 || dataForm.name == 3 || dataForm.name == 4 || dataForm.name == 14 || dataForm.name == 15 
            || dataForm.name == 19 || dataForm.name == 22 || dataForm.name == 23 || dataForm.name == 24 || dataForm.name == 25 || dataForm.name == 26 || dataForm.name == 27">
              <a-form-model-item :label="$t('支付渠道')">
                <a-select v-model="dataForm.payChannel">
                  <a-select-option v-for="(item, index) in collectionList" :key="index" :value="item.value">{{
                    item.text
                  }}</a-select-option>
                </a-select>
              </a-form-model-item>
            </a-row>
            <a-row v-if="taxSetting == 1">
              <a-form-model-item :label="$t('税率')" prop="taxRate">
                <a-input-number
                  v-model="dataForm.taxRate"
                  style="width: calc(100% - 1em);"
                  :disabled="dataForm.ivalue == '0'"
                  :min="0"
                  :precision="4"
                />%
              </a-form-model-item>
            </a-row>
          </a-form-model>
        </a-col>
      </a-row>
    </div>
  </j-modal>
</template>

<script>
import { httpAction, postAction, getAction } from '@/api/manage'
import { filterMultiDictText, initDictOptions } from '@/components/dict/JDictSelectUtil'
import pick from 'lodash.pick'
export default {
  name: 'EditModal',
  components: {},
  data() {
    return {
      model: {},
      title: '',
      visible: false,
      dataForm: {
        name_dictText: '',
        payChannel: '0',
      },
      labelCol: {
        xs: { span: 20 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 14 },
      },
      collectionList: [],
      payType_state: undefined,
      rules: {
        nameText: [{ required: true, message: this.$t('请输入支付方式') }],
        taxRate: [{ required: true, message: this.$t('请输入') }]
      },
      taxSetting: localStorage.getItem("taxSetting")
    }
  },
  mounted() {},
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add () {
      this.edit(this.modelDefault);
    },
    edit(record) {
      let that = this
      that.taxSetting = localStorage.getItem("taxSetting")
      that.visible = true
      initDictOptions('bis_pay_channel.name').then((res) => {
        if (res.success) {
          that.collectionList = res.result
        }
      })
      if(record.name == 8 || record.name == 9 || record.name == 13 || parseInt(record.name) > 900 || record.name === undefined) {
        that.payType_state = false
      }else{
        that.payType_state = true
      }
      that.dataForm = Object.assign({}, record)
    },
    handleOk() {
      let that = this
      let url;
      if (!that.dataForm.id) {
        url = '/pay/bisPayType/add'
      } else {
        url = '/pay/bisPayType/edit'
      }
      that.$refs.form.validate((valid) => {
        if (valid) {
          postAction(url, that.dataForm).then((res) => {
            if (res.success) {
              that.$message.success(res.message)
              that.close()
              that.$emit('ok')
            } else {
              that.$message.warning(res.message)
            }
          })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
.bold-font {
  font-weight: bold;
}
</style>